package com.dc.chat.handler.exception;

import com.dc.chat.entity.to.ResponseBean;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestControllerAdvice
public class GlobalExceptionHandler{
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseBean handlerMethodArgumentNotValidException(MethodArgumentNotValidException e){
        StringBuffer errors=new StringBuffer();
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        for (int i=fieldErrors.size()-1;i>=0;i--){
            errors.append(fieldErrors.get(i).getDefaultMessage()+"\n");
        }
        return  ResponseBean.failed(errors.toString());
    }

}
